<?php

namespace App\Controller\Api\Consultant;

use App\Constants\ErrorCode;
use App\Controller\Api\BaseController;
use App\Helper\HashHelper;
use App\Model\User;
use App\Service\UserService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Psr\Http\Message\ResponseInterface;

/**
 * 咨询师端用户登录
 * @Controller(prefix="/consultant/v1/auth")
 */
class AuthController extends BaseController
{


    /**
     * @Inject()
     * @var UserService
     */
    protected $userService;



    /**
     * 邮箱手机号登录
     * @RequestMapping(path="login")
     * @return ResponseInterface
     */
    public function login()
    {

        $params = $this->request->post();
        $this->validate($params, [
            'account' => 'required',
            'password' => 'required',
            'accountType' => 'required',
        ]);

        $where[] = ['phone','=',$params['account']] ;
        $where[] = ['email','=',$params['account'],'or'] ;

        $userInfo = User::where($where)->first();

        if (is_null($userInfo)){
            return $this->response->returnFail(ErrorCode::LOGIN_USER_NULL);
        }


        if (!password_verify($params['password'], $userInfo->password)) {
            return $this->response->returnFail(ErrorCode::LOGIN_PASSWORD);
        }

        // todo 会员状态是否要 封禁状态


        return $this->userService->consultantUserLogin($userInfo['id'], $params['platform']??1, $params['accountType']);


    }


    /**
     * @RequestMapping(path="register")
     * @return void
     */
    public function register()
    {

        $params = $this->request->post();

        $this->validate($params, [
            'account' => 'required',
            'accountType' => 'required',
            'password' => 'required',
            'code' => 'required'
        ]);

        $rel = $this->checkRegisterCode($params['code'], $params['account']);
        if (!$rel) {
            return $this->response->returnFail(ErrorCode::REGISTER_CODE_FAIL);
        }


        if (!in_array($params['accountType'], ['phone', 'email'])) {
            return $this->response->returnFail();
        }


        $existsWhere[] = [$params['accountType'], '=', $params['account']];

        $exists = User::where($existsWhere)->exists();

        if ($exists) {
            return $this->response->returnFail(ErrorCode::REGISTER_ALREADY);
        }


        $baseInfo = [
            $params['accountType'] => $params['account'],
            'type' => 1,
            'create_time' => time(),
            'last_login_time' => time(),
            'num' => User::makeNum(),
            'nickname' => getRandomString(6),
            'password' => HashHelper::make($params['password'])
        ];

        $uid = User::addRegisterUser($baseInfo);


        if ($uid){
            return $this->userService->consultantUserLogin($uid, $params['platform']??1, 'wechat');
        }else{
            return $this->response->returnFail(ErrorCode::REGISTER_FAIL);
        }


    }


    /**
     * 验证验证码是否有效
     * @param $account
     * @param $code
     * @return true
     */
    public function checkRegisterCode($account, $code)
    {


        return true;

    }


}